package com.telit.android.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.telit.android.dto.MsgInfoDTO;
import com.telit.android.dto.TrackInfoDTO;
import com.telit.android.dto.UpAndDownLineDTO;
import com.telit.common.constant.MsgIdentificationConstants;
import com.telit.common.entity.DataReturn;
import com.telit.common.util.RedisUtils;
import com.telit.communicate.dto.MsgProcessPathDTO;
import com.telit.communicate.dto.SendMessageDTO;
import com.telit.communicate.feign.RemoteCommunicateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


/**
 * @author zhouyu
 * @description 安卓端websocket消息处理
 */
@RestController
public class AppMessageProcessController implements ApplicationRunner {

    @Autowired
    private RemoteCommunicateService communicateFeignClient;

    @Override
    public void run(ApplicationArguments args) {
        //单独启动线程，防止项目启动失败
        new Thread(() -> {
            MsgProcessPathDTO msgProcessPathDTO = new MsgProcessPathDTO();
            msgProcessPathDTO.setSystemName("FIREPROOF");
            msgProcessPathDTO.setMsgSource("PHONE");
            msgProcessPathDTO.setMessageProcessPath("http://yichang-android/android/messageProcess");
            msgProcessPathDTO.setUpAndDownProcessPath("http://yichang-android/android/upAndDownProcess");
            communicateFeignClient.subscribeMsgProcess(msgProcessPathDTO);
        }).start();
        //服务异常结束 回调方法取消订阅
        Runtime.getRuntime().addShutdownHook(new Thread(()->{
            MsgProcessPathDTO msgProcessPathDTO = new MsgProcessPathDTO();
            msgProcessPathDTO.setSystemName("FIREPROOF");
            msgProcessPathDTO.setMsgSource("PHONE");
            communicateFeignClient.unSubscribeMsgProcess(msgProcessPathDTO);
        }));
    }

    @PostMapping(value = "/messageProcess", produces = "application/json;charset=UTF-8")
    public DataReturn messageProcess(@RequestBody MsgInfoDTO msgInfoDTO){
        TrackInfoDTO trackInfoDTO = JSONObject.parseObject(msgInfoDTO.getMsg(), TrackInfoDTO.class);
        RedisUtils.setBoundHash(String.valueOf(trackInfoDTO.getTaskId()),msgInfoDTO.getUserId().split("_")[0],JSON.toJSONString(trackInfoDTO));
        //推送消息到web端
        SendMessageDTO sendMessageDTO = new SendMessageDTO();
        sendMessageDTO.setFlag("web");
        sendMessageDTO.setMsg(JSON.toJSONString(DataReturn.ok(MsgIdentificationConstants.FOREST_RANGERS_REAL_TIME_TRACKING, trackInfoDTO)));
        communicateFeignClient.sendMsg(sendMessageDTO);
        return DataReturn.ok();
    }

    @PostMapping(value = "/upAndDownProcess", produces = "application/json;charset=UTF-8")
    public DataReturn upAndDownProcess(@RequestBody UpAndDownLineDTO upAndDownLineDTO){
        Map<String, Object> loginOn = new HashMap<>(2);
        loginOn.put("id", upAndDownLineDTO.getUserId());
        loginOn.put("status", upAndDownLineDTO.getUpAndDown());
        //推送消息到web端
        SendMessageDTO sendMessageDTO = new SendMessageDTO();
        sendMessageDTO.setFlag("web");
        sendMessageDTO.setMsg(JSON.toJSONString(DataReturn.ok(MsgIdentificationConstants.FOREST_RANGERS_PATROL_SIGN, loginOn)));
        communicateFeignClient.sendMsg(sendMessageDTO);
        return DataReturn.ok();
    }



}
